﻿using System.Text;

namespace RSPDS.Core
{
    public static class RSPDS_CamlBuilder
    {
        #region Nested Classes

        public class IRSPDS_Caml { }

        public class RSPDS_CamlAnd : IRSPDS_Caml
        {
            public IRSPDS_Caml Left { get; set; }
            public IRSPDS_Caml Right { get; set; }

            public override string ToString()
            {
                return string.Format("<And>{0}{1}</And>", Left, Right);
            }
        }

        public class RSPDS_CamlOr : IRSPDS_Caml
        {
            public IRSPDS_Caml Left { get; set; }
            public IRSPDS_Caml Right { get; set; }

            public override string ToString()
            {
                return string.Format("<Or>{0}{1}</Or>", Left, Right);
            }
        }

        public abstract class RSPDS_CamlCompare : IRSPDS_Caml
        {
            public abstract string Operator { get; }
            public string FieldName { get; set; }
            public string ValueType { get; set; }
            public string Value { get; set; }

            public override string ToString()
            {
                var sb = new StringBuilder();
                sb.AppendFormat("<{0}><FieldRef Name=\"{1}\"/>", Operator, FieldName);
                sb.AppendFormat("<Value Type=\"{0}\">{1}</Value></{2}>", ValueType, Value, Operator);
                return sb.ToString();
            }
        }

        public class RSPDS_CamlEq : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "Eq"; }
            }
        }

        public class RSPDS_CamlNeq : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "Neq"; }
            }
        }

        public class RSPDS_CamlGt : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "Gt"; }
            }
        }

        public class RSPDS_CamlGeq : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "Geq"; }
            }
        }

        public class RSPDS_CamlLt : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "Lt"; }
            }
        }

        public class RSPDS_CamlLeq : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "Leq"; }
            }
        }

        public class RSPDS_CamlBeginsWith : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "BeginsWith"; }
            }
        }

        public class RSPDS_CamlContains : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "Contains"; }
            }
        }

        public class RSPDS_CamlIsNull : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "IsNull"; }
            }
        }

        public class RSPDS_CamlIsNotNull : RSPDS_CamlCompare
        {
            public override string Operator
            {
                get { return "IsNotNull"; }
            }
        }

        #endregion Nested Classes

        public static string GetQuery(IRSPDS_Caml caml)
        {
            return string.Format("<Where>{0}</Where>", caml);
        }

    }
}
